package com.baibei.widget.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.baibei.widget.R;
import com.rae.widget.dialog.impl.SlideDialog;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.List;


/**
 * 分享对话框
 * 记得要在程序初始化的时候设置友盟分享
 * Created by ChenRui on 2017/1/24 0024 14:14.
 */
public class ShareDialog extends SlideDialog implements View.OnClickListener {


    /**
     * cb
     */
    public interface OnShareClickListener {

        /**
         * 分享成功
         *
         * @param dialog dialog
         */
        void onShare(Dialog dialog);

        /**
         * 分享失败
         *
         * @param msg msg
         */
        void onShareFailed(Dialog dialog, String msg);
    }

    View mWeChatView;
    View mWeChatSNSView;
    ImageView mQRCodeView;
    ShareAction mShareAction;

    private OnShareClickListener mOnShareClickListener;


    public ShareDialog(Context context) {
        super(context);
        setContentView(R.layout.dialog_blog_content);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mWeChatView = findViewById(R.id.tv_share_wechat);
        mWeChatSNSView = findViewById(R.id.tv_share_wechat_sns);

        mWeChatSNSView.setOnClickListener(this);
        mWeChatView.setOnClickListener(this);
        findViewById(R.id.btn_share_cancel).setOnClickListener(this);
        mShareAction = new ShareAction((Activity) context);


    }

    @Override
    public void show() {
        super.show();
        if (getWindow() != null)
            getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }

    public void setShareTitle(String title) {
        mShareAction.withText(title);
    }

    public void setShareUrl(String url) {
        mShareAction.withMedia(new UMWeb(url));
    }

    public void setInvitationCode(String invitationCode) {
    }

    public void setShareWeb(@NonNull String url, @NonNull String title, @Nullable String desc, String thumb) {
        UMWeb web = new UMWeb(url);
        web.setTitle(title);
        if (!TextUtils.isEmpty(desc)) {
            web.setDescription(desc);
        }
        if (!TextUtils.isEmpty(thumb)) {
            web.setThumb(new UMImage(getContext(), thumb));
        } else {
            int icon = getContext().getApplicationInfo().icon;
            web.setThumb(new UMImage(getContext(), icon));
        }
        mShareAction.withMedia(web);

    }


    public void setShareWeb(@NonNull String url, @NonNull String title, @Nullable String desc, @Nullable int thumbResId) {
        UMWeb web = new UMWeb(url);
        web.setTitle(title);
        web.setDescription(desc);
        web.setThumb(new UMImage(getContext(), thumbResId));
        mShareAction.withMedia(web);
    }

    public void setShareSummary(String summary) {
        mShareAction.withText(summary);
    }

    public void setShareIcon(int resId) {
        mShareAction.withMedia(new UMImage(getContext(), resId));
    }

    public void setShareIcon(String url) {
        mShareAction.withMedia(new UMImage(getContext(), url));
    }

    public void setOnShareClickListener(OnShareClickListener onShareClickListener) {
        mOnShareClickListener = onShareClickListener;
    }

    /**
     * 调用友盟分享
     */
    protected void share(SHARE_MEDIA type) {
        //判断--是否安装微信
        if ((type == SHARE_MEDIA.WEIXIN || type == SHARE_MEDIA.WEIXIN_CIRCLE) && !isThirdClientInstall(getContext(), "com.tencent.mm")) {
            if (mOnShareClickListener != null) {
                mOnShareClickListener.onShareFailed(this, "请先安装微信客户端");
            }
            return;
        }
        if (mOnShareClickListener != null) {
            mOnShareClickListener.onShare(this);
        }

        mShareAction.setPlatform(type);
        mShareAction.share();
    }


    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.tv_share_wechat) {
            share(SHARE_MEDIA.WEIXIN);
        } else if (i == R.id.tv_share_wechat_sns) {
            share(SHARE_MEDIA.WEIXIN_CIRCLE);
        }

        dismiss();
    }

    /**
     * 是否有微信
     *
     * @param context context
     * @return return
     */
    public boolean isThirdClientInstall(Context context, String packageId) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals(packageId)) {
                    return true;
                }
            }
        }
        return false;
    }

}
